﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;
using System.Data;
using System.Configuration;

public partial class Computers_Computers : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            loadValues();
            MultiView1.ActiveViewIndex = 0;
            Panel3.Visible = false;
            Panel4.Visible = false;
            Panel6.Visible = false;
            LinkButtona.ForeColor = System.Drawing.Color.Black;

        }

        PersonDBConnection partConn = new PersonDBConnection();
        GridView2.DataSourceID = null;
        GridView2.DataSource = partConn.getPersonsNotAuthorizedDataSet(Int32.Parse(txtComputerID.Text));
        GridView2.DataBind();

        SoftwareDBConnection softwareConn = new SoftwareDBConnection();
        GridView1.DataSourceID = null;
        GridView1.DataSource = softwareConn.getSoftwareNotInstalledDataSet(Int32.Parse(txtComputerID.Text));
        GridView1.DataBind();

    }

    private void loadValues()
    {
        int computer_ID = Int32.Parse((String)Session["Computer_ID"]);
        LinkButton6.Text = "Computer("+computer_ID+")";
        ComputerDBConnection computerConn = new ComputerDBConnection();
        Computer computer = computerConn.getComputerByID(computer_ID);

        HardwareDBConnection hardwareConn = new HardwareDBConnection();
        Hardware hardware = hardwareConn.getHardwareByID(computer.hardware_ID);

        txtComputerID.Text = computer.computer_ID.ToString();
        txtComputerName.Text = computer.computer_Name;
        txtLicense.Text = computer.license;
        txtSerial.Text = hardware.serial;
        txtPurchaseDate.Text = hardware.purchase_date.ToString();

        //PersonDBConnection personConn = new PersonDBConnection();
        //Person person = personConn.getPersonByIDno(hardware.person_ID_no);
        //txtPerson.Text = person.firstname + " " + person.surname;
        
        //txtLocation.Text = hardware.room_no;

        RoomDBConnection roomConn = new RoomDBConnection();
        Room classroom = roomConn.getRoomByNo(hardware.room_no);

        txtLocation.Text = classroom.room_Name;

        WarrantyDBConnection warrantyConn = new WarrantyDBConnection();
        Warranty warranty = warrantyConn.getWarrantyById(hardware.warranty_ID);

        //txtWarrenty.Text = warranty.warranty_Status;
        hlWarrenty.Text = warranty.warranty_Status;

        loadGridView(computer.computer_ID);
        loadInstalledSoftwareGridView(computer.computer_ID);
        loadGridViewPersons(computer.hardware_ID);



        lbAssetHeading.Text = "Computer Asset: " + computer.computer_Name + "(" + computer.computer_ID + ")";
    }


    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        ComputerDBConnection computerConn = new ComputerDBConnection();
        Computer computer = computerConn.getComputerByID(Int32.Parse(txtComputerID.Text));

        computer.computer_Name = txtComputerName.Text;
        computer.license = txtLicense.Text;
        computerConn.update(computer);







        RoomDBConnection roomConn = new RoomDBConnection();
        Room room = roomConn.getRoomByName(ddlLocation.SelectedValue);
        
        HardwareDBConnection hardwareConn = new HardwareDBConnection();
        Hardware hardware = hardwareConn.getHardwareByID(computer.hardware_ID);
        hardware.room_no = room.room_No;
        hardwareConn.update(hardware);
        txtLocation.Text = room.room_Name;
        //hardware.person_ID_no = 


        //hardware.purchase_date = DateTime.Parse(txtPurchaseDate.Text);
        //hardware.serial = txtSerial.Text;
        //hardware.warranty_ID = Int32.Parse(txtWarrenty.Text);
        //hardware.update(hardware);

        //dbConnection.update(customer);

        setEditableFalse();
        btnUpdate.Visible = false;
        btnCancle.Visible = false;
        btnEdit.Visible = true;
        btnDelete.Visible = true;


        txtLocation.Visible = true;
        ddlLocation.Visible = false;
        lbtnLocation.Visible = false;
        //lbtnPerson.Visible = false;
    }
    protected void btnCancle_Click(object sender, EventArgs e)
    {
        setEditableFalse();
        btnUpdate.Visible = false;
        btnCancle.Visible = false;
        btnEdit.Visible = true;
        btnDelete.Visible = true;


        txtLocation.Visible = true;
        ddlLocation.Visible = false;
        lbtnLocation.Visible = false;
        //lbtnPerson.Visible = false;
    }
    protected void btnEdit_Click(object sender, EventArgs e)
    {
        setEditableTrue();
        btnUpdate.Visible = true;
        btnCancle.Visible = true;
        btnEdit.Visible = false;
        btnDelete.Visible = false;

        txtLocation.Visible = false;
        ddlLocation.Visible = true;
        lbtnLocation.Visible = true;
        //lbtnPerson.Visible = true;
    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        ComputerDBConnection computerConn = new ComputerDBConnection();
        Computer computer = computerConn.getComputerByID(Int32.Parse(txtComputerID.Text));

        HardwareDBConnection hardwareConn = new HardwareDBConnection();
        Hardware hardware = hardwareConn.getHardwareByID(computer.hardware_ID);
        hardwareConn.delete(hardware);

        FeedBack feedback = new FeedBack();
        feedback.success = 1;
        feedback.lastPageURL = "Assets.aspx";
        feedback.message = "You have successfully deleted the asset: '" +
                            computer.computer_Name + " (" + computer.computer_ID + ")' of type computer asset.";
        Session["Feedback"] = feedback;
        Server.Transfer("../Feedback.aspx");

    }


    private void setEditableFalse()
    {
        txtComputerID.ReadOnly = true;
        txtComputerName.ReadOnly = true;
        txtLicense.ReadOnly = true;
        txtLocation.ReadOnly = true;
        //txtPerson.ReadOnly = true;
        txtPurchaseDate.ReadOnly = true;
        txtSerial.ReadOnly = true;
        //txtWarrenty.ReadOnly = true;

        txtComputerID.BorderStyle = BorderStyle.None;
        txtComputerName.BorderStyle = BorderStyle.None;
        txtLicense.BorderStyle = BorderStyle.None;
        txtLocation.BorderStyle = BorderStyle.None;
        //txtPerson.BorderStyle = BorderStyle.None;
        txtPurchaseDate.BorderStyle = BorderStyle.None;
        txtSerial.BorderStyle = BorderStyle.None;
        //txtWarrenty.BorderStyle = BorderStyle.None;
    }
    private void setEditableTrue()
    {
        //txtComputerID.ReadOnly = false;
        txtComputerName.ReadOnly = false;
        //txtLicense.ReadOnly = false;
        txtLocation.ReadOnly = false;
        //txtPerson.ReadOnly = false;
        //txtPurchaseDate.ReadOnly = false;
        //txtSerial.ReadOnly = false;
        //txtWarrenty.ReadOnly = false;

        //txtComputerID.BorderStyle = BorderStyle.NotSet;
        txtComputerName.BorderStyle = BorderStyle.NotSet;
        //txtLicense.BorderStyle = BorderStyle.NotSet;
        txtLocation.BorderStyle = BorderStyle.NotSet;
        //txtPerson.BorderStyle = BorderStyle.NotSet;
        //txtPurchaseDate.BorderStyle = BorderStyle.NotSet;
        //txtSerial.BorderStyle = BorderStyle.NotSet;
        //txtWarrenty.BorderStyle = BorderStyle.NotSet;
    }

    private void loadGridViewPersons(int hardware_ID)
    {

        OleDbConnection myOleDbConnection = new OleDbConnection();
        //string connectionString = "provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|Database.accdb;";
        myOleDbConnection.ConnectionString = ConfigurationManager.ConnectionStrings["AssetDbConnection"].ConnectionString;

        OleDbCommand myOleDbCommand = myOleDbConnection.CreateCommand();

        myOleDbConnection.Open();
        OleDbDataAdapter da = new OleDbDataAdapter("Select Person.ID_No, Firstname, Surname, Telephone, CellPhone FROM [Person] INNER JOIN [Asset_Allocation] ON (Person.ID_No = Asset_Allocation.ID_No) WHERE Hardware_ID = "+hardware_ID.ToString()+" ;", myOleDbConnection);
        DataSet ds = new DataSet();
        da.Fill(ds, "PersonTable");

        personGridView.DataSource = ds;
        personGridView.DataBind();

        myOleDbConnection.Close();
    }




    private void loadGridView(int computer_ID)
    {

        OleDbConnection myOleDbConnection = new OleDbConnection();
        //string connectionString = "provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|Database.accdb;";
        myOleDbConnection.ConnectionString = ConfigurationManager.ConnectionStrings["AssetDbConnection"].ConnectionString;

        OleDbCommand myOleDbCommand = myOleDbConnection.CreateCommand();

        myOleDbConnection.Open();
        OleDbDataAdapter da = new OleDbDataAdapter("Select Part_ID AS [Part ID], Part_Type, Part_Description FROM PART WHERE Computer_ID = " + computer_ID + ";", myOleDbConnection);
        DataSet ds = new DataSet();
        da.Fill(ds, "CustomersTable1");

        partGridView.DataSource = ds;
        partGridView.DataBind();

        myOleDbConnection.Close();
    }


    private void loadInstalledSoftwareGridView(int computer_ID)
    {

        OleDbConnection myOleDbConnection = new OleDbConnection();
        //string connectionString = "provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|Database.accdb;";
        myOleDbConnection.ConnectionString = ConfigurationManager.ConnectionStrings["AssetDbConnection"].ConnectionString;

        OleDbCommand myOleDbCommand = myOleDbConnection.CreateCommand();

        myOleDbConnection.Open();
        OleDbDataAdapter da = new OleDbDataAdapter("SELECT Software.Software_ID, Software_Name, Brand_Name, Update_Date "
                                                  + "FROM [Installed_Software] INNER JOIN [Software] ON (Software.Software_ID = Installed_Software.Software_ID)"
                                                  + "WHERE Computers_ID =" + computer_ID + ";", myOleDbConnection);
        DataSet ds = new DataSet();
        da.Fill(ds, "CustomersTable1");

        installedSoftwareGridView.DataSource = ds;
        installedSoftwareGridView.DataBind();

        myOleDbConnection.Close();


    }

    protected void installedSoftwareGridView_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        GridViewRow row = installedSoftwareGridView.Rows[e.NewSelectedIndex];

        Session["Software_ID"] = row.Cells[1].Text;
        Response.Redirect("../Software/Software.aspx", true);
    }
    protected void installedSoftwareGridView_RowCreated(object sender, GridViewRowEventArgs e)
    {

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes["onmouseover"] = "this.style.cursor='pointer';this.style.textDecoration='underline';this.style.color='Blue';";
            e.Row.Attributes["onmouseout"] = "this.style.textDecoration='none';this.style.color='Black';";
            e.Row.ToolTip = "Click to select row";
            e.Row.Attributes["onclick"] = this.Page.ClientScript.GetPostBackClientHyperlink(this.installedSoftwareGridView, "Select$" + e.Row.RowIndex);
        }
    }
    protected void partGridView_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes["onmouseover"] = "this.style.cursor='pointer';this.style.textDecoration='underline';this.style.color='Blue';";
            e.Row.Attributes["onmouseout"] = "this.style.textDecoration='none';this.style.color='Black';";
            e.Row.ToolTip = "Click to select row";
            e.Row.Attributes["onclick"] = this.Page.ClientScript.GetPostBackClientHyperlink(this.partGridView, "Select$" + e.Row.RowIndex);
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (Panel3.Visible)
        {
            Panel3.Visible = false;
        }
        else
        {
            Panel3.Visible = true;
        }

    }


    protected void TabChange(object sender, EventArgs e)
    {
        LinkButton lb = (LinkButton)sender;
        if (lb.ID == "LinkButtona")
        {
            MultiView1.ActiveViewIndex = 0;
            lb.ForeColor = System.Drawing.Color.Black;
            LinkButton2.ForeColor = System.Drawing.Color.White;
            LinkButton3.ForeColor = System.Drawing.Color.White;
        }
        else if (lb.ID == "LinkButton2")
        {
            MultiView1.ActiveViewIndex = 1;
            lb.ForeColor = System.Drawing.Color.Black;
            LinkButtona.ForeColor = System.Drawing.Color.White;
            LinkButton3.ForeColor = System.Drawing.Color.White;
        }
        else if (lb.ID == "LinkButton3")
        {
            MultiView1.ActiveViewIndex = 2;
            lb.ForeColor = System.Drawing.Color.Black;
            LinkButton2.ForeColor = System.Drawing.Color.White;
            LinkButtona.ForeColor = System.Drawing.Color.White;
        }
    }


    protected void WarrantyLinkButton_Click(object sender, EventArgs e)
    {
        ComputerDBConnection computerConn = new ComputerDBConnection();
        Computer computer = computerConn.getComputerByID(Int32.Parse(txtComputerID.Text));

        HardwareDBConnection hardwareConn = new HardwareDBConnection();
        Hardware hardware = hardwareConn.getHardwareByID(computer.hardware_ID);

        Session["Warranty_ID"] = hardware.warranty_ID;
        Response.Redirect("../Warranty/Warranty.aspx", true);
    }

    protected void LinkButton1_Click(object sender, EventArgs e)
    {

    }
    protected void intallPartGridView_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        GridViewRow row = intallPartGridView.Rows[e.NewSelectedIndex];
        int partID  = Int32.Parse(row.Cells[1].Text);

        PartDBConnection partConn = new PartDBConnection();
        Part part = partConn.getUninstalledPartByID(partID);

        part.computer_ID = Int32.Parse(txtComputerID.Text);

        partConn.installUpdate(part);
        Response.Redirect("Computers.aspx", true);
    }
    protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        GridViewRow row = GridView1.Rows[e.NewSelectedIndex];
        int softwareID = Int32.Parse(row.Cells[1].Text);

        int computerID = Int32.Parse(txtComputerID.Text);

        Session["softwareID"] = softwareID;
        Session["computerID"] = computerID;

        Response.Redirect("../InstalledSoftware/InstalledSoftware.aspx", true);

    }

    protected void intallPartGridView_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            LinkButton db = (LinkButton)e.Row.Cells[0].Controls[0];

            db.OnClientClick = "return confirm('Are you certain you want to add this part to this computer?');";
        }
    }

    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            LinkButton db = (LinkButton)e.Row.Cells[0].Controls[0];

            db.OnClientClick = "return confirm('Are you certain you want to install this software on this computer?');";
        }
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        if (Panel4.Visible)
        {
            Panel4.Visible = false;
        }
        else
        {
            Panel4.Visible = true;
        }
    }

    protected void GridView2_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        GridViewRow row = GridView2.Rows[e.NewSelectedIndex];
        String personID = row.Cells[1].Text;

        ComputerDBConnection computerConn = new ComputerDBConnection();
        Computer computer = computerConn.getComputerByID(Int32.Parse(txtComputerID.Text));


        PersonDBConnection personConn = new PersonDBConnection();
        personConn.insetAssetAllocation(personID, computer.hardware_ID);
        Response.Redirect("Computers.aspx", true);
    }

    protected void GridView2_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //LinkButton db = (LinkButton)e.Row.Cells[0].Controls[0];

            //db.OnClientClick = "return confirm('Are you certain you want to allow this person to use this computer?');";
        }
    }
    protected void Button4_Click(object sender, EventArgs e)
    {
        if (Panel6.Visible)
            Panel6.Visible = false;
        else
            Panel6.Visible = true;
    }
    protected void partGridView_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        GridViewRow row = partGridView.Rows[e.NewSelectedIndex];
        int partID = Int32.Parse(row.Cells[1].Text);

        Session["Part_ID"] = partID;
        Session["hasComptuerID"] = true;
        Response.Redirect("../Parts/Parts.aspx");
    }
    protected void LinkButton6_Click(object sender, EventArgs e)
    {
        Session["Computer_ID"] = txtComputerID.Text;
    }
}